// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }) // 使用当前云环境
const db = cloud.database();
const collection = "category";
// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  //分类数据
  let {
    data,
    type
  } = event;
  if(type=="cateNav"){
    let res2 = await db.collection(collection).where({
      type: 'cateNav'
    }).get()
    if (!res2.data.length) {
      db.collection('category').add({
        data: {
          type,
          data
        },
        success: function (res) {
          console.log('wwwww');
        }
      })
    }
  }else if(type=="secNav"){
    db.collection(collection).add({
      data: {
        type,
        data
      }
    });
  }
  return {
    event,
    openid: wxContext.OPENID,
    appid: wxContext.APPID,
    unionid: wxContext.UNIONID,
  }
}